package algorithms.que13;

import edu.princeton.cs.algs4.In;

/**
 * @功能
 * @创建时间 2020/7/13
 */
public class JosePhus<Item> {
    private int N=0;
    private Integer[] array;
    //数组重构
    private void resize(int max,Integer d){
        Integer[] temp = new Integer[max];
        for (int i = 0; i < N+1; i++) {
            if(i<d){
            temp[i] = array[i];}
            else if(i>d){
                if(i==0){i=1;
                }
                temp[i-1] = array[i];}
        }
        array = temp;
    }
    //再继续循环
    public JosePhus(Integer N,Integer M) {
        array=new Integer[N];
        Integer tmp=M;
        this.N=N;
        for (int i=0;i<N;i++){
            array[i]=i;
        }  for (int i=0;i<N;i++){
            if(tmp>this.N);
            tmp=tmp%this.N;
            if(tmp==0){
                tmp=this.N;
            }
            Integer p = del(tmp);
            tmp=tmp+M-1;
            System.out.println(p);}
    }
    //取出人数
    public Integer del(Integer p){
        Integer i=array[p-1];
        N--;
        resize( N,p-1);
        return i;
    }
}
